// pages/contact/contact.js
import env from "../../utils/env";
const app = getApp();
var inputVal = '';
var msgList = [];
var windowWidth = wx.getSystemInfoSync().windowWidth;
var windowHeight = wx.getSystemInfoSync().windowHeight;
var keyHeight = 0;
let conversation_id = '';


/**
 * 计算msg总高度
 */
// function calScrollHeight(that, keyHeight) {
//   var query = wx.createSelectorQuery();
//   query.select('.scrollMsg').boundingClientRect(function(rect) {
//   }).exec();
// }

Page({
  /**
 * 页面的初始数据
 */
  data: {
    logs: [],
    message: '',
    disabled: false,
    plain: false,
    downloaddisabled: false,
    downloadloading: false,
    loading: false,
    downloadUrl: '',
    playlistid: '',
    result: '',
    q: '',
    dialogShow: false,
    maxClickCount: 3, //定义允许最大的点击次数，超过次数需要有分享或者其他逻辑处理
    scrollHeight: '100vh',
    inputBottom: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 获取聚焦
   */
  focus: function (e) {
    keyHeight = e.detail.height;
    this.setData({
      scrollHeight: (windowHeight - keyHeight) + 'px'
    });
    this.setData({
      toView: 'msg-' + (msgList.length - 1),
      inputBottom: keyHeight + 'px'
    })
    //计算msg高度
    // calScrollHeight(this, keyHeight);
  },
  //失去聚焦(软键盘消失)
  blur: function (e) {
    this.setData({
      scrollHeight: '100vh',
      inputBottom: 0
    })
    this.setData({
      toView: 'msg-' + (msgList.length - 1)
    })
  },
  checkClickAuth(maxClickCount) {
    let isShared = wx.getStorageSync('isShared')
    console.log("isshared==", isShared)
    if (!isShared) {
      console.log("no shared..")
      let clickCountKey = 'clickCount'
      let clickCount = wx.getStorageSync(clickCountKey)
      if (!clickCount || clickCount == 0) {
        clickCount = 1
      } else {
        clickCount = clickCount + 1
      }
      wx.setStorageSync(clickCountKey, clickCount)
      console.log("clickCount===", clickCount)
      if (clickCount > maxClickCount) {
        return false
      }
      return true
    }
    return true
  },
  resetData() {
    console.log("reset clickCount to 0")
    let clickCountKey = 'clickCount'
    wx.setStorageSync(clickCountKey, 0)
    wx.setStorageSync('isShared', 1)
  },
  tapDialogButton(e) {
    console.log("tap dailog info", e)
    this.setData({
      dialogShow: false,
    })

    if (e.detail.index == 1) {
      console.log("点击分享按钮")
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
    }
  },
  buttontap(e) {
    console.log(e.detail)
    console.log("点击分享按钮")
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  onShareAppMessage(options) {
    return {
      title: '泰世茂证件照',
      imageUrl: '',
      path: 'pages/main/index'
    }
  },
  onShareTimeline(options) {
    return {
      title: '泰世茂证件照',
      imageUrl: '',
      path: 'pages/main/index'
    }
  },
  goToIndex: function () {
    app.login();
    wx.navigateTo({
      url: "/pages/home/index",
    })
  }
  //   let pass = this.checkClickAuth(this.data.maxClickCount);
  //   pass = true;
  //   if (!pass) {
  //     wx.showModal({
  //       title: '分享给好友',
  //       content: '分享一个好友可增加三次体验次数哦',
  //       showCancel: true,
  //       success(res) {
  //         if (res.confirm) {
  //           console.log('用户点击确定')
  //         } else if (res.cancel) {
  //           console.log('用户点击取消')
  //         }
  //       }
  //     })
  //     return;
  //   } else {
  //     if (wx.getStorageSync('is_login')) {
  //       wx.navigateTo({
  //         url: "/pages/home/index",
  //       })
  //     } else {
  //       app.login();
  //       wx.navigateTo({
  //         url: "/pages/home/index",
  //       })
  //     }
  //   }
  // }
})